package com.loyal.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.loyal.helper.DialogHelper;
import com.loyal.helper.ErrorHelper;
import com.loyal.helper.UIHelper;
import com.loyal.impl.IFrag2ActImpl;
import com.loyal.impl.StatusBarImpl;
import com.loyal.impl.ToolBarBackListener;
import com.loyal.impl.UICommandImpl;
import com.loyal.kit.ResUtil;
import com.loyal.kit.StateBarUtil;
import com.loyal.ui.fragment.BasicFragment;

public abstract class BasicActivity extends AppCompatActivity implements UICommandImpl, IFrag2ActImpl, StatusBarImpl {

    public @LayoutRes int actLayoutRes() {
        return 0;
    }

    public abstract void afterOnCreate();

    private ToolBarBackListener backListener;

    @Override
    public boolean isFullScreen() {
        return false;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isFullScreen()) {
            hideActionBar();
        }
        setLayoutOrView();
        statusBar();
        afterOnCreate();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && isFullScreen()) {
            UIHelper.fullScreenImmersive(getWindow());
        }
    }

    /**
     * 使用mvvm框架时，需注意按照以下顺序：
     * binding = DataBindingUtil.setContentView(this, actLayoutRes());
     * ButterKnife.bind(this)；
     * 使用ViewBinding框架，要将setContentView(actLayoutRes());或者super.setViewByLayoutRes()注释掉，
     * binding = ActivityMainBinding.inflate(layoutInflater)
     * val view = binding.root
     * setContentView(view)
     */
    public void setLayoutOrView() {
    }

    @Override
    public void hideActionBar() {
        if (null != getActionBar()) getActionBar().hide();
        if (null != getSupportActionBar()) getSupportActionBar().hide();
    }

    @Override
    public void statusBar() {
        StateBarUtil.setTranslucentStatus(this, isFullScreen());//沉浸式状态栏
    }

    @Override
    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        super.setSupportActionBar(toolbar);
        setActionBack(true);
    }

    public void setActionBack(boolean showHomeAsUp) {
        ActionBar actionBar = getSupportActionBar();
        if (null != actionBar) {
            actionBar.setDisplayHomeAsUpEnabled(showHomeAsUp);
            actionBar.setHomeButtonEnabled(showHomeAsUp);
        }
    }

    public void setToolbarBackListener(ToolBarBackListener listener) {
        backListener = listener;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            if (null != backListener) backListener.onBack();
            else finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void showToast(@NonNull CharSequence sequence) {
        initToast(sequence);
    }

    @Override
    public void showErrorToast(@NonNull CharSequence sequence, Throwable e) {
        String error = ErrorHelper.getMessage(e);
        showToast(replaceText(sequence) + "(" + error + ")");
    }

    @Override
    public void showDialog(@NonNull CharSequence sequence) {
        showDialog(sequence, false);
    }

    @Override
    public void showDialog(@NonNull CharSequence sequence, boolean finish) {
        showMessageDialog(sequence, finish);
    }

    @Override
    public String replaceText(CharSequence sequence) {
        return ResUtil.replaceText(sequence);
    }

    @Override
    public void showErrorDialog(@NonNull CharSequence sequence, Throwable e) {
        showErrorDialog(sequence, e, false);
    }

    @Override
    public void showErrorDialog(@NonNull CharSequence sequence, Throwable e, boolean finish) {
        String error = ErrorHelper.getMessage(e);
        showDialog(replaceText(sequence) + "(" + error + ")", finish);
    }

    private void initToast(final CharSequence sequence) {
        runOnUiThread(() -> UIHelper.toastShort(this, sequence));
    }

    public void showMessageDialog(CharSequence content, final boolean isFinish) {
        DialogHelper.showInActivity(this, content, isFinish);
    }

    /**
     * {@link BasicFragment#onAttach(Context)}
     */
    @Override
    public void onFrag2Act(String tag, @Nullable Object objTag) {
    }
}
